// packageE/membertool/membertool.js
var app = getApp();
Component({
  properties: {
    datas: {
      type: null
    },
    component_id: {
      type: null
    }
  },
  // 私有数据，可用于模板渲染
  data: {
    emptyImage:  `${app.globalData.imgPath}image.png`,

    activeNames: true,
    TemplateArray_1: [],
    TemplateArray_2: []
  },
  lifetimes: {
    // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
    attached() {},
    moved() {},
    detached() {},
  },

  // 生命周期函数，可以为函数，或一个在methods段中定义的方法名
  attached() {},
  // 此处attached的声明会被lifetimes字段中的声明覆盖
  ready() {
  },

  methods: {
    errorFunction(event) {
      let index = event.currentTarget.dataset.index;
      let img = 'datas.show_list[' + index + '].image';
      this.setData({
        [img]:  `${app.globalData.imgPath}image.png`,
      });
    }, 
    //获取模板id
    getTemplate_1(name) {
      let urlStr = app.getNetAddresss("member.member.getMiniTemplateCorrespond");
      let that = this;
      let uid = wx.getStorageSync('yz_uid')||0;
      app._getNetWork({
        url: urlStr,
        data: {
          small_type: 4
        },
        success: function(resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            let array = [];
            for (let i = 0; i < res.data.length; i++) {
              array.push(res.data[i].template_id);
            }
            that.setData({
              TemplateArray_1: array
            });
            
            wx.requestSubscribeMessage({
              tmplIds: that.data.TemplateArray_1,
              success(res) {
                if(name == 'supplier'){
                  wx.navigateTo({
                    url: '/packageA/member/supplier/supplier',
                  })
                }else{
                  wx.navigateTo({
                    url: '/packageA/member/SupplierCenter/SupplierCenter?uid=' + uid
                  });
                }
              },
              fail() {
                if(name == 'supplier'){
                  wx.navigateTo({
                    url: '/packageA/member/supplier/supplier',
                  })
                }else{
                  wx.navigateTo({
                    url: '/packageA/member/SupplierCenter/SupplierCenter?uid=' + uid
                  });
                }
              }
            });
          } else {
            wx.showToast({
              title: res.msg,
              icon: 'none',
              duration: 1500
            });
          }
        },
        fail: function(res) {
          console.log(res);
        }
      });
    },
    //获取模板id
    getTemplate_2() {
      let urlStr = app.getNetAddresss("member.member.getMiniTemplateCorrespond");
      let that = this;
      app._getNetWork({
        url: urlStr,
        data: {
          small_type: 5
        },
        success: function(resdata) {
          var res = resdata.data;
          if (res.result == 1) {
            let array = [];
            for (let i = 0; i < res.data.length; i++) {
              array.push(res.data[i].template_id);
            }
            that.setData({
              TemplateArray_2: array
            });
            wx.requestSubscribeMessage({
              tmplIds: that.data.TemplateArray_2,
              success(res) {
                wx.navigateTo({
                  url: '/packageC/o2o/storeManage/storeManage'
                });
              },
              fail() {
                wx.navigateTo({
                  url: '/packageC/o2o/storeManage/storeManage'
                });
              }
            });
          } else {
            wx.showToast({
              title: res.msg,
              icon: 'none',
              duration: 1500
            });
          }
        },
        fail: function(res) {
          console.log(res);
        }
      });
    },
    //根据后台返回名字做跳转
    pluginGoto(e) {
      let item = e.currentTarget.dataset.item;
      let url = item.url;
      // //跳转到门店管理
      if (item.url == 'storeManage') {
        this.gostoreManage();
        return;
      }
      //跳转到推广中心
      if (item.url == 'extension') {
        this.goextension();
        return;
      }

      if (item.name == "courier") {
        wx.setStorage({
          key: 'couriername',
          data: item.title
        });
        wx.navigateTo({
          url: '/packageA/member/' + url + '/' + url
        });
      } else if (item.name == "supplier" || item.name == "SupplierCenter") {
        this.getTemplate_1(item.url);
      } else if (item.url == "HotelManage") {
        wx.navigateTo({
          url: '/packageA/hotel/HotelManage/HotelManage?title=' +
					encodeURIComponent("酒店管理")
        });
      } else if (item.url == 'aftersaleslist') {
        this.goaftersaleslist();
      } else if (item.url == 'helperLiveList') {
        this.getTemplate();
      }else if (item.url == 'subsidiary') {
        wx.navigateTo({
          url: '/packageA/member/myOrder_v2/myOrder_v2?status=0&orderType=subsidiary'
        });
      }  else if (item.url == 'cloudIndex') {
        wx.navigateTo({
          url: '/packageE/others/cloudwarehouse/cloud_index/cloud_index'
        });
      } else {
        let mini_url = item.mini_url;
        if(item.page_data && item.page_data.data_url) {
          mini_url = mini_url + `?api=${item.page_data.data_url}`;
        }
        if(item.page_data && item.page_data.date_time) {
          mini_url = mini_url + `&date_time=1`;
        }
        if(item.page_data && item.page_data.extparams) {
          mini_url = mini_url + `&extparams=${ JSON.stringify(item.page_data.extparams)}` 
        }
        if(item.page_data && item.page_data.plu) {
          mini_url = mini_url + `${item.page_data.data_url ? '&' : '?'}plu=${item.page_data.plu}`;
        }
        if(item.page_data && item.page_data.id) {
          mini_url = mini_url + `?id=${item.page_data.id}`;
        }
        wx.navigateTo({
          url: mini_url
        });
      } 
    },
    goaftersaleslist: function() {
      wx.navigateTo({
        url: '/packageD/member/myOrder/Aftersaleslist/Aftersaleslist'
      });

    },

    //跳转到门店管理
    gostoreManage() {
      this.getTemplate_2();
      //storeManage
    },


    goextension: function() {
      //extension
      wx.reLaunch({
        url: '/packageG/pages/member/extension/extension'
      });
    },
    onChange(event) {
      this.setData({
        activeNames: !this.data.activeNames
      });
    },
    gotoUrl(e) {
      let url = e.currentTarget.dataset.item.h5_link;
      try {
        wx.navigateTo({
          url: url
        });
      } catch (e) {
        wx.showToast({
          title: '链接设置错误',
          icon: 'none',
          duration: 2000
        });
      }
    }
  }
});
